package com.findMoneyGame.pages.game.controllers {
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFieldAutoSize;

	import com.findMoneyGame.pages.shop.ImprovementsConfig;
	import com.findMoneyGame.pages.game.GameLevelPage;
	import com.findMoneyGame.pages.game.controllers.Controller;
	import com.findMoneyGame.tools.TextFormatProvider;

	public class GameTimerController extends Controller {
		private var timer : Timer;
		private var sandClockTimer : Timer;
		private var maxTopIndicatorHeight : int;
		private var maxBottomIndicatorHeight : int;
		private var maxSandLineHeight : int;
		private var timerLabel : TextField;
		private var _seconds : int;

		public function get seconds () { return timer.currentCount; }

		private function get bottomIndicator () { return sandClock.bottomIndicator; }
		private function get topIndicator () { return sandClock.topIndicator; }
		private function get sandLine () { return sandClock.sandLine; }
		private function get sandClock () { return gameLevelPage.sandClock; }
		private function get timeTable () { return gameLevelPage.timeTable; }
		private function get bonusTime () { return ImprovementsConfig.getTimeBonus(user.timeBonusIndex); }

		public function GameTimerController (gameLevelPage : GameLevelPage) {
			super(gameLevelPage);
			timeTable.addChild(timerLabel = initTextField('0:0:0'));
			(timer = new Timer(1000)).addEventListener(TimerEvent.TIMER, timerTick);

			maxTopIndicatorHeight = topIndicator.height;
			maxBottomIndicatorHeight = bottomIndicator.height;
			maxSandLineHeight = sandLine.height;

			bottomIndicator.height = 0;
			
			sandClock.maxMoney.label.text = gameLevel.data.bonusTimeReward;
			sandClock.minMoney.label.text = gameLevel.data.reward;

			(sandClockTimer = new Timer(100, bonusTime * 10)).addEventListener(TimerEvent.TIMER, sandClockTimerTick);

			gameLevelPage.addEventListener(GameLevelPage.STOP, stop);
			gameLevelPage.addEventListener(GameLevelPage.START, start);
		}

		private function start (e : Event) {
			timer.start();
			sandClockTimer.start();
		}
		private function stop (e : Event) {
			timer.stop();
			sandClockTimer.stop();
		}
		private function timerTick (e : Event) {
			if (timer.currentCount == bonusTime && gameLevel.data.reward > 0) {
				gameLevel.data.reward -= gameLevel.data.bonusTimeReward
			}
			var s : int = timer.currentCount % 60;
			var m : int = (timer.currentCount % 3600) / 60;
			var h : int = timer.currentCount / 3600;
			timeTable.removeChild(timerLabel);
			timeTable.addChild(timerLabel = initTextField(h + ':' + m + ':' + s));
		}
		private function sandClockTimerTick (e : Event) {
			var currentCount = sandClockTimer.currentCount;
			var repeatCount = sandClockTimer.repeatCount;
			var delayCount = repeatCount * 0.1;
			var repeatCountForHalf = repeatCount - delayCount;
			if (0 <= currentCount && currentCount <= repeatCountForHalf) {
				var topHalfPercent = 1 - currentCount / repeatCountForHalf;
				topIndicator.height = maxTopIndicatorHeight * topHalfPercent;
			}
			if (0 <= currentCount && currentCount <= delayCount) {
				var sandLinePercent = currentCount / delayCount;
				sandLine.height = maxSandLineHeight * sandLinePercent;
			}
			if (delayCount <= currentCount && currentCount <= repeatCount) {
				var bottomHalfPercent = (currentCount - delayCount) / repeatCountForHalf;
				bottomIndicator.height = maxBottomIndicatorHeight * bottomHalfPercent;
			}
			if (repeatCountForHalf <= currentCount && currentCount <= repeatCount) {
				var delta = (maxSandLineHeight - maxBottomIndicatorHeight) / (repeatCount - repeatCountForHalf);
				sandLine.height = sandLine.height - delta;
				sandLine.y = sandLine.y + delta;
			}
		}
		private function initTextField (s : String) : TextField {
			var format = TextFormatProvider.getFormat(20, 0xFFFFFF);
			var text = new TextField();
			text.x = 15;
			text.y = 20;
			text.width = 70;
			text.height = 70;
			text.autoSize = TextFieldAutoSize.LEFT;
			text.selectable = false;
			text.setTextFormat(format);
			text.defaultTextFormat = format;
			text.text = s;
			return text;
		}
	}
}